<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <canvas id="cont" width="500px" , height="500px">您的浏览器版本太低，不支持画布，请更新浏览器</canvas>
    <script>
        //固定的两步
        var canvas = document.querySelector('#cont')
        var ctx = canvas.getContext('2d')

        drawLine(100, 100, 400, 100, 'blue', 10)
        drawLine(100, 400, 400, 400, 'yellow', 10)
        drawLine(400, 100, 400, 400, 'green', 10)
        drawLine(100, 100, 100, 400, 'red', 10)

        function drawLine(x1, y1, x2, y2, color, width) {
            ctx.beginPath();
            ctx.moveTo(x1, y1)
            ctx.lineTo(x2, y2)
            ctx.strokeStyle = color
            ctx.lineWidth = width
            ctx.stroke()
            ctx.closePath()
        }

    </script>
</body>

</html>